package com.yvon.service.demo.jdk.juc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 自定义生产者-消费者模式
 *
 * @author : Yvon
 * @since : 2022-05-17
 */
public class TestConsumer {
    public static void main(String[] args) {
        BlockingQueue<Object> queue = new ArrayBlockingQueue<>(10);
        MyBlockingQueueForCondition queue1 = new MyBlockingQueueForCondition(16);

        Runnable producer = () -> {
            while (true) {
                try {
                    queue1.put(new Object());
                    System.out.println("存1个对象进队列，队列数量："+ queue1.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(producer).start();
        new Thread(producer).start();

        Runnable consumer = () -> {
            while (true) {
                try {
                    queue1.take();
                    System.out.println("取1个对象出队列，队列数量："+ queue1.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(consumer).start();
        new Thread(consumer).start();
    }
}
